/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package wave;

import ast.*;
import base.*;
import java.util.Hashtable;
import java.util.Vector;
/**
 *
 * @author Tue
 */

public class WaveExecution {
    protected KN kn;
    protected SynchronousQueue<Wave> waveQueue;
    protected WaveEnvironment waveEnv;
    protected WaveInterpreter wi;
    protected Thread currentThread;

    public WaveExecution(WaveHeadParser parser, WaveEnvironment waveEnv) {
        this.kn = parser.getKn();
        this.waveQueue = parser.getWaveQueue();
        this.waveEnv = waveEnv;
        this.wi = parser.getWi();
        currentThread = parser;
    }
    public Vector getASTNodeValue(SimpleNode node){
        Vector v = new Vector();
        Hashtable<String, Vector> frontalTable = waveEnv.getFrontalTable();
        Hashtable<String, Vector> nodalTable = new Hashtable<String, Vector>();
        if(waveEnv.getCONTENT() != null){
            nodalTable = kn.getKNNode(waveEnv.getCONTENT()).getNodalTable(waveEnv.getColor());
        }

        if(waveEnv.getCONTENT() != null){
            KNNode knNode = kn.getKNNode(waveEnv.getCONTENT());
            if(knNode != null)
                nodalTable = kn.getKNNode(waveEnv.getCONTENT()).getNodalTable(waveEnv.getColor());
        }
        
        if(node instanceof ASTFrontalVar){
            v = frontalTable.get(node.getString());
        } else if(node instanceof ASTNodalVar){
            v = nodalTable.get(node.getString());
        } else if(node instanceof ASTStringConst){           
            String value = node.getString().trim();
            if(value.startsWith("`"))
                value = value.substring(1, value.length() - 1);
            if(isInteger(value)) v.add(Integer.parseInt(value));
            else v.add(value);
        } else if(node instanceof ASTIntConst){
            String value = node.getString();            
            if(isInteger(value)) v.add(Integer.parseInt(value));
            else v.add(value);
        } else if(node instanceof ASTTraveledLink){
            if(waveEnv.getLINK() != null){
                String link = waveEnv.getLINK();
                if(isInteger(link)){
                    v.add(Integer.parseInt(link));
                } else {
                    v.add(link);
                }
            }
        } else if(node instanceof ASTContent){
            if(waveEnv.getCONTENT() != null)
                v.add(waveEnv.getCONTENT());
        } else if(node instanceof ASTPredecessor){
            if(waveEnv.getPREDECESSOR() != null)
                v.add(waveEnv.getPREDECESSOR());
        } else if(node instanceof ASTArrayConst){
            for(int i=0;i<node.jjtGetNumChildren(); i++)
                v.addAll(getASTNodeValue((SimpleNode)node.jjtGetChild(i)));
        } else if(node instanceof ASTNamedLink){
            SimpleNode child = (SimpleNode)node.jjtGetChild(0);
            if(child instanceof ASTStringConst){
                String value = node.getString().trim();
                if(value.startsWith("`"))
                    value = value.substring(1, value.length() - 1);
                if(isInteger(value)) v.add(Integer.parseInt(value));
                else v.add(value);
            } else {
                return getASTNodeValue((SimpleNode) node.jjtGetChild(0));
            }
        } else v.add(node.getString());
        return v != null?v:(new Vector());
    }
    private boolean isInteger(String value){
        try{
            Integer.parseInt(value);
            return true;
        } catch(Exception ex){
            return false;
        }
    }
}
